
 /*---------------

 PULL.C - Generalized pulldown menu

 ---------------*/

 #include <stdio.h>
 #include <conio.h>

 #include <keys.c>
 #include <util.h>
 #include <util.c>

 #ifndef SPACING
   #define SPACING  1
 #endif

 #define MT_POPUP     1
 #define MT_MENUITEM  2

 #define MS_HORIZONTAL  1
 #define MS_VERTICAL    2

 #define FALSE  0
 #define TRUE   1

 typedef struct pulldown
  {
    char *optname ;
    int prval ;
    struct pulldown *dpoptions ;
   } PULLDOWN ;

 PULLDOWN temp[] =
  {
    "load", 1, NULL,
    "pick", 2, NULL,
    "new", 3, NULL,
    "save", 4, NULL,
    "write to", 5, NULL,
    "directory",6, NULL,
    "change dir",7, NULL,
    "os shell",8, NULL,
    "quit",9, NULL
   } ;

 PULLDOWN top[] =
  {
    "File",0,temp,
    "Edit",1,NULL,
    "Run",2,NULL,
    "Compile",3,NULL,
    "Project",4,NULL,
    "Options",5,NULL,
    "Debug",6,NULL,
    "Break/watch",7,NULL
   } ;

 int Display (row,col,lines,bkc,pull)
 int row , col , lines , bkc ;
 PULLDOWN pull[] ;
  {
    int i , currcol ;

    for ( i = 0 , currcol = col ; i < lines ; i++ )
     {
       PokeStrAttr (row,currcol,pull[i].optname,bkc) ;
       currcol = currcol + SPACING + strlen (pull[i].optname) ;
      }



    return i ;
   }

 int pulldown (row,col,pull,lines,bkc)
 int row , col , lines , bkc ;
 PULLDOWN pull[] ;
  {
    int i , currcol , k ;
    char c ;
    static int prev ;

    for ( k = col , i = 0 ; i < prev ; i++ )
      k += SPACING + strlen (pull[i].optname) ;

    CAttribRect (row,col,1,strlen (pull[i].optname),FrWHITE) ;

    do
     {
       c = getch () ;
       if ( c == 0 )
         c = getch () ;

       switch ( c )
        {
          case UP :
          case LEFT :
               if ( i == 0 )
                {
                  CAttribRect (row,k,1,strlen (pull[i].optname),bkc) ;
                  for ( i = 0 ; i < ( lines - 1 ) ; i++ )
                    k += SPACING + strlen (pull[i].optname) ;
                  CAttribRect (row,k,1,strlen (pull[i].optname),FrWHITE) ;
                  break ;
                 }
               if ( i > 0 )
                {
                  CAttribRect (row,k,1,strlen (pull[i].optname),bkc) ;
                  i-- ;
                  k = k - SPACING - strlen (pull[i].optname) ;
                  CAttribRect (row,k,1,strlen (pull[i].optname),FrWHITE) ;
                 }
               break ;

          case DOWN :
          case RIGHT :
               if ( i == ( lines - 1 ) )
                {
                  CAttribRect (row,k,1,strlen (pull[i].optname),bkc) ;
                  k = col ;
                  i = 0 ;
                  CAttribRect (row,k,1,strlen (pull[i].optname),FrWHITE) ;
                  break ;
                 }
               if ( i < ( lines - 1 ) )
                {
                  CAttribRect (row,k,1,strlen (pull[i].optname),bkc) ;
                  k += SPACING + strlen (pull[i].optname) ;
                  i++ ;
                  CAttribRect (row,k,1,strlen (pull[i].optname),FrWHITE) ;
                 }
               break ;
         }
      }
    while ( ( c != ESC ) && ( c != RET ) ) ;

    CAttribRect (row,k,1,strlen (pull[i].optname),bkc) ;
   }

 main ()
  {
    char c ;

    Display (1,1,8,BrWHITE,top) ;
    do
     {
       c = getch () ;
       if ( c == 0 )
         c = getch () ;

       switch ( c )
        {
          case F10 :
               pulldown (1,1,top,8,BrWHITE) ;
         }
      }
    while ( c != ESC ) ;
   }
